package com.ruan.service;

import com.ruan.dao.CartMapper;
import com.ruan.entity.Cart;
import com.ruan.entity.Result;
import com.ruan.openfeign.GoodsCenterOpenfeign;
import com.ruan.query.CartQuery;
import com.ruan.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName:CartService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/2 19:41
 **/
@Service
public class CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private GoodsCenterOpenfeign goodsCenterOpenfeign;

    public Result addCart(Cart cart) {
        int row = cartMapper.addCart(cart);
        if(row != 1){
            return new Result(100,"购物车添加失败");
        }else {
            return new Result(200,"购物车添加成功");
        }
    }

    public List<CartVo> getCartList(CartQuery cartQuery) {
        List<CartVo> cartVoList = cartMapper.getCartList(cartQuery);
        for(CartVo i:cartVoList){
            if(goodsCenterOpenfeign.getGoodsById(i.getGoodsId())==null){
                i.setGoodsName("该商品已下架");
            }else {
                i.setGoodsName(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsName());
                i.setGoodsDesc(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsDesc());
                i.setGoodsPrize(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsPrize());
                i.setGoodsPicture(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsPicture());
                i.setGoodsType(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsType());
                i.setGoodsTypeId(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsTypeId());
                i.setTotalPrice(goodsCenterOpenfeign.getGoodsById(i.getGoodsId()).getGoodsPrize()*i.getCartCount());
            }
        }
        return cartVoList;
    }

    public Result deleteCart(Integer id) {
        int row = cartMapper.deleteCart(id);
        if(row != 1){
            return new Result(100,"购物车删除失败");
        }else {
            return new Result(200,"购物车删除成功");
        }
    }

    public Result deleteCartByIds(Integer[] ids) {
        int row = cartMapper.deleteCartByIds(ids);
        if(row != ids.length){
            return new Result(100,"购物车删除失败");
        }else {
            return new Result(200,"购物车删除成功");
        }
    }

    public Result editCart(Cart cart) {
        int row = cartMapper.editCart(cart);
        if(row != 1){
            return new Result(100,"购物车添加失败");
        }else {
            return new Result(200,"购物车添加成功");
        }
    }

    public void deleteCartByUserId(Integer id) {
        cartMapper.deleteCartByUserId(id);
    }

    public void deleteCartByUserIds(Integer[] ids) {
        cartMapper.deleteCartByUserIds(ids);
    }
}
